<?php
use \Illuminate\Support\Facades\Cache;

if (! function_exists('gm_time')) {

    /**
     * 获得当前格林威治时间的时间戳
     * @author Jerry
     * @return false|int|string
     */
    function gm_time()
    {
        return (time() - date('Z'));
    }
}


if (! function_exists('local_to_time')) {

    /**
     * 将GMT时间戳转化为当前设置时间的时间戳
     * @author Jerry
     * @param $time
     * @return false|float|int|string
     */
    function local_to_time($time)
    {
        $timezone = config('shidexian.default_time_zone');

        if (empty($time)) {
            $time = gm_time();
        }

        $time += ($timezone * 3600);

        return $time;
    }
}


if (! function_exists('get_queue_goods_stock')) {
    /**
     * 获取商品队列缓存
     * @param $goods_id
     * @param $wid
     * @return mixed
     */
    function get_queue_goods_stock($goods_id,$wid){
        $goods_stock = Cache::get('queue_goods_stock_'.$goods_id.'_'.$wid);
        if(empty($goods_stock)){
            $goods_stock = Cache::get('goods_practical_num_cache_'.$goods_id.'_'.$wid);
            Cache::forever('queue_goods_stock_'.$goods_id.'_'.$wid,$goods_stock);
        }
        return $goods_stock;
    }
}


if (! function_exists('set_queue_goods_stock')) {
    /**
     * 记录商品队列缓存
     * @param $goods_id
     * @param $wid
     * @param $num
     */
    function set_queue_goods_stock($goods_id,$wid,$num){
        Cache::forever('queue_goods_stock_'.$goods_id.'_'.$wid,$num);
    }
}

if (! function_exists('price_format')) {
    /**
     * 格式化商品价格
     *
     * @access  public
     * @param   float $price 商品价格
     * @return  string
     */
    function price_format($price, $change_price = true)
    {
        if ($price === '') {
            $price = 0;
        }
        $price = 0 + $price;//添加这一行，转换成数值
        if ($change_price) {
            switch (4) {
                case 0:
                    $price = number_format($price, 2, '.', '');
                    break;
                case 1: // 保留不为 0 的尾数
                    $price = preg_replace('/(.*)(\\.)([0-9]*?)0+$/', '\1\2\3', number_format($price, 2, '.', ''));

                    if (substr($price, -1) == '.') {
                        $price = substr($price, 0, -1);
                    }
                    break;
                case 2: // 不四舍五入，保留1位
                    $price = substr(number_format($price, 2, '.', ''), 0, -1);
                    break;
                case 3: // 直接取整
                    $price = intval($price);
                    break;
                case 4: // 四舍五入，保留 1 位
                    $price = number_format($price, 1, '.', '');
                    break;
                case 5: // 先四舍五入，不保留小数
                    $price = round($price);
                    break;
            }
        } else {
            $price = number_format($price, 2, '.', '');
        }

        $price = sprintf('￥%s', $price);

        return $price = strip_tags($price);
    }
}

if (! function_exists('get_price')) {
    function get_price($price){
        if($price>0){
            $res = round($price,2);
        }else{
            $res = 0;
        }
        return number_format($res, 2, '.', '');
    }
}



if (! function_exists('local_date')) {
    /**
     * @author Jerry
     * @param $format
     * @param null $time
     * @return false|string
     */
    function local_date($format, $time = NULL) {
        $timezone = config('shidexian.default_time_zone');

        if ($time === NULL) {
            $time = gm_time();
        } elseif ($time <= 0) {
            return '';
        }
        $time += ($timezone * 3600);
        return date($format, $time);
    }
}

if (! function_exists('convert_to_time')) {
    /**
     * 转换时分结构字符串（例：09:30）
     * @param $time
     * @return string
     */
    function convert_to_time($time){
        if(!empty($time)){
            if(strlen($time)<4){
                $time = '0'.strval($time);
            }
            $time = substr($time,0,2).":".substr($time,2,2);
        }

        return $time;
    }
}

if (! function_exists('local_str_to_time')) {

    /**
     * 将一个用户自定义时区的日期转为GMT时间戳
     * @author Jerry
     * @param string $time
     * @return false|float|int
     */
    function local_str_to_time(string $time)
    {
        $timeZone = config('shidexian.default_time_zone');

        return strtotime($time) - ($timeZone * 3600);
    }
}

if (! function_exists('cdn_asset')) {

    /**
     * 生成cdn地址
     * @author Jerry
     * @param string $path
     * @param string $style
     * @return string
     */
    function cdn_asset(string $path, string $style = '')
    {
        return config('shidexian.cdn_url') . '/' . $path . $style;
    }
}


if (! function_exists('get_order_sn')) {
    /**
     * 得到新订单号
     * @return  string
     */
    function get_order_sn() {
        /* 选择一个随机的方案 */
        mt_srand((double) microtime() * 1000000);

        return date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
    }
}

if (! function_exists('tree_data')) {
    /**
     * 树形数组
     * @author Min
     * @param $data
     * @param int $pid
     * @return array
     */
    function tree_data($data, $pid = 0){
        $result = array();
        foreach($data as $v){
            if($v['parent_id'] == $pid){
                $v['children'] = tree_data($data,$v['cat_id']);
                $result[] = $v;
            }
        }
        return $result;
    }
}



